/**
 * Copyright (c) 2016-2100 yunclouds
 */
package com.yunqian.web;

import com.yunqian.redot.constant.BusinessType;
import com.yunqian.redot.constant.ShowStatus;
import com.yunqian.redot.domain.Contents;
import com.yunqian.redot.domain.Information;
import com.yunqian.redot.domain.InformationType;
import com.yunqian.redot.domain.dto.InformationDto;
import com.yunqian.redot.service.ContentsService;
import com.yunqian.redot.service.InformationService;
import com.yunqian.redot.service.InformationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * Describe: 咨询文章控制器
 *
 * @author zsy
 * 2018-09-29 15:54:34
 */
@RestController
@RequestMapping("/api/information")
public class InformationController {

    @Autowired
    private InformationService informationService;
    @Autowired
    private ContentsService contentsService;
    @Autowired
    private InformationTypeService informationTypeService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<Information> pagination(@RequestBody Map<String, Object> params) {
        params.put("_filter_eq_status", ShowStatus.YES.getCode());
        return informationService.pagination(params);
    }

    /**
     * 获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public InformationDto detail(@PathVariable("id") Long id) {
        Information information = informationService.findStrictOne(id);
        List<Contents> contentsList = contentsService.findAllByRelateBusinessAndRelateId(BusinessType.INFORMATION.getCode(), information.getId());
        InformationType type = informationTypeService.getFirstById(information.getTypeId());
        InformationDto dto = new InformationDto();
        dto.setInformation(information);
        dto.setContentsList(contentsList);
        dto.setTypeName(type.getTitle());
        return dto;
    }

}
